package org.epidroid.epidroid.androsoul.ui.configuration;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.androsoul.engine.service.AndroSoulService;
import org.epidroid.epidroid.androsoul.engine.service.AndroSoulServiceBinder;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceActivity;

/**
 * @author conrath Activity that is launched when the users clicks on the
 *         Settings button
 */
public class ConfigurationActivity extends PreferenceActivity {

	private IAndroSoulService _service = null;
	private ServiceConnection _serviceConnection = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.androsoul_configuration);
		bindService();
	}

	@Override
	protected void onDestroy() {
		unbindService(_serviceConnection);
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		ConfigurationUpdater.process(_service, getBaseContext());
		super.onPause();
	}

	private void bindService() {
		Intent intent = new Intent(this, AndroSoulService.class);

		_serviceConnection = new ServiceConnection() {
			public void onServiceConnected(ComponentName name,
					IBinder servicebinder) {
				_service = ((AndroSoulServiceBinder) servicebinder)
						.getService();
			}

			public void onServiceDisconnected(ComponentName name) {
			}
		};

		bindService(intent, _serviceConnection, Context.BIND_AUTO_CREATE);
	}
}